<?php
namespace App\Adshopuser\Models;
use Common\BaseModel;

class BShopSkuValue extends BaseModel
{
    public $res;
    public $expire;

    public function initialize()
    {
        parent::initialize();
        $this->setSource('b_shop_sku_value');
    }



    public function keyValueList($vid)
    {
        $phql   = "SELECT 
                        b.id as value_id,
                        b.value_name,
                        a.key_name,
                        a.id as key_id
                FROM b_shop_sku_key AS a LEFT JOIN b_shop_sku_value AS b ON b.key_id = a.id WHERE a.id IN ($vid)";
        $rows = $this->getDI()->get('dbMaster')->fetchAll($phql);
        foreach ($rows as $val) {
            $tmp[$val['key_name'] . '-' . $val['key_id']][] = $val;
        }
        $result = [];
        foreach ($tmp as $key => $val) {
            $keyIds            = explode('-', $key);
            $kTemp             = [];
            $kTemp['key_name'] = $keyIds[0];
            $kTemp['key_id']   = $keyIds[1];
            foreach ($val as $vVal) {
                $valTemp['value_id']   = $vVal['value_id'];
                $valTemp['value_name'] = $vVal['value_name'];
                $kTemp['value'][]      = $valTemp;
            }
            $result[] = $kTemp;
        }
        return $result;
    }


    public function getValueName($value_ids)
    {
        $id                  = implode(',', explode('_', $value_ids));
        $where['conditions'] = "id in ($id)";
        $valueArr            = $this->find($where)->toarray();
        $sku_name            = '';
        if (!empty($valueArr)) {
            $sku_name = implode(' ', array_column($valueArr, 'value_name'));
        }
        return $sku_name;
    }

}
